package com.learn.algorlthm;

/**
 * @classname:
 * @Description:调整数组顺序使奇数位于偶数前面
 * @author: yfpu(fengpu.yang @ ucarinc.com)
 * @Date: 2018-07-24 15:52
 *
 * 题目描述：
 * 需要保证奇数和奇数，偶数和偶数之间的相对位置不变，这和书本不太一样
 */
public class Learn21 {

    public static void reOrderArray(int[] nums) {
        if (nums == null || nums.length == 0)
            return;
        //奇数个数
        int oddCnt = 0;
        for (int num : nums) {
            if (num%2 == 1)
                oddCnt++;
        }
        int[] copy = nums.clone();
        int i = 0, j = oddCnt;
        for (int num : copy) {
            if (num%2 == 1)
                nums[i++] = num;
            else
                nums[j++] = num;
        }
    }

}
